package com.lwlean.test.file;

import org.apache.commons.codec.binary.Base64;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.FileCopyUtils;
import sun.misc.BASE64Encoder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Created by lean on 2016/12/9.
 */
public class FileEncryption {

    final private Logger log = LoggerFactory.getLogger(this.getClass());

    final private static String[] baseArray = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d",
            "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
            "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
            "y", "z", "0", "1", "2", "3", "4", "5", "6", "7",
            "8", "9", "+", "/", "\n", "\r"};


    @Test
    public void fileEncryTest(){
        final String filePath = "C:\\Users\\lean\\Desktop\\text2audio.mp3";
        final String generalFilePaht = "C:\\Users\\lean\\Desktop\\text3audio.mp3";
        File file = new File(filePath);
        String name = file.getName();
        log.info("this name is :{}",name);
        log.info("file length is :{}",file.length());

        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] buffer = new byte[(int)file.length()];
            fileInputStream.read(buffer);
            fileInputStream.close();
            String filecode = new BASE64Encoder().encode(buffer);
            log.info("general filecode is : {}",filecode);

            byte[] speech = Base64.decodeBase64(filecode);
            FileCopyUtils.copy(speech,new File(generalFilePaht));
            File generateFile = new File(generalFilePaht);
            log.warn("generate file name is :{}",generateFile.getName());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testBase64String(){

        String baseStr = "//MoxAAMkFXMAUYQAIiP7ve93bGEEHAAQgXB8oD854gGgg4P0ygYu//znD8MFAQl1A+/Lvrf/wxE" +
                "7xG8Mf/l1cni2JfiHFfw//MoxAgPQdZkAYo4AAeBf+AQNRL/wAAGCwS//QgAWDsn/+QU8HAkEP/8" +
                "943G55D//8xpjDcP//rKQfLh93//nH4Qg8hkZUAM//MoxAYNWx6wAYEoAJHZrbXIyf6fT/b/////" +
                "//+9Prcg9H//9J1f7mVjh61W//9UWW9GQ5JXRbDTOUgWyv/8/akUo/DfJeuF//MoxAsP4kKsAc8Q" +
                "AAVV9olLxomb/GGb//////////S7KqIzHQJHnu/et/nQjrs5zAyDkWCFkcLfZpiZYjKs+b66+moV" +
                "8A3D//MoxAYOuaa4AIQElHIcaITC4iVJEcwdYk4Nix2DaLp4wUmgpbPQQQX/////////dZyEV3Zb" +
                "gjKWzLWSr2qc0L8b+mXQEoRN//MoxAYOcIasAKYeSJJoRjzGqzIBFMASbRa1oiDRZdf/ESN0A1s/" +
                "//gmARpgJFg0hrxUFbOWFg4drI85/8Rf/kmQpOy20WWi//MoxAcNuKLiXmvUToCXqgbUzocd/FRs" +
                "lJuK3PsGC2HztygA4n+o9Dmt2k5+RyrtZ3+e/8toUbX////8Oreu8znHmMMRz6GE//MoxAsNEHKE" +
                "AN7YSBYombA03MS5CaYIEjxCECNb3paPHvKJehH6veoFyYRAhP1+tAW8//7a6k4f59SxoADUE5RZ" +
                "XQl///////MoxBERSja0ymhGmJ//pecyDCUrQYcIM7/oVNCIOLAA4BUUgQVvSnFYp/5MR+5//UcJ" +
                "FYKLQWCYcCxn/6zQE/RV///7P/uD//MoxAYMKbK0AAAGlG0qBlFkSI56dWqcJlHFm6ECKv+HJHxC" +
                "DxGgSCEwkWC0ocATdP/+zbgg0ML/sRjuhgjC1QhmBXrs6ItD//MoxBAMkXa8AAAElKYUNcRhE640" +
                "WPHxhp4WI0lAbNTQqKZ5FQ6j8VZk1YECroaq+t6pPgLQEYGrx1DsL596aloGZFzczdtf//MoxBgN" +
                "OQqwAIwQcE4cAKDv8gyDCz4SYbj+L/gWPOLDBT65X/zv8RcpDkkkkcAEgAHyFW8VtHJXMcoaS1+t" +
                "/+UgVaaZ2p0X//MoxB4MGKLqXg6iTlJjcAF4saWHCJL////+n4kd8woXQ7STbba4AfVn0A8B0+VI" +
                "qT+JYHjz2+azY5CZlVTN/KUpxXM1+H+n//MoxCgMaNr+XjqYcur9/2/V9Tvv+LX9BqA+wE8DfwA8" +
                "QuUydfmCAs0ZEmlJo+xCCwiK++mAoKHhYGzs4p8IhYCDEwyHyinQ//MoxDENUNqsAIzKcMXPwsXV" +
                "+taDqI8DjwGzIgnPRd4Cwn/5QaE//M0W9kGnEWFESPIFGD4AAYqG9hcGyOtHdnHOh6mBw+h+//Mo" +
                "xDYO4X6oAJHKlLe7ps3ehfrL6SJMjNAEIgU7F1JfWorHuExAnv1qQH8TshN+YEuSY5xvsZ7ihLGy" +
                "las3+WUKM/qV/SIG//MoxDUMUVqoAKNElF4WWGegb1AHKr87r8HrLrmEaUyG0ix/86j+OwxAEwpM" +
                "f7LsHBgybAkO0ffHLhIIA8AUB+QjUCdJADaf//MoxD4MOMaoAKYYcTs5f83fgwqFWk81f/28fZOk" +
                "SoaRq/LGzsjNsGmwTCT//////r/61wKXE7bQALaB+mgC0hZPS1IjLBAC//MoxEgM+Kq1dFYeTMT+" +
                "0MGimDtRt63byJY1YyZ3/4c2un/////9YU/0e3qViAwDC/HA20UwEIPQpXNLFciGQinv2gva///F" +
                "//MoxE8M0MbmXmwechPGkqiYklOZvmlZXv//////8qCv//1qQBBDjAgEk0HNGARIpUbiJseNHekT" +
                "ivTGjmNVPc0nE9S0xOax//MoxFYMWMa2R0p4ACi2PyIWFVKOh1EgcG4sDo4Le9O61xDsTs5g0zW6" +
                "G6QQvNoxnvEWmi79Zz+e0pehFX//5/ScjDTCMjNo//MoxF8U2XpoNZJQABC9AshbJzIJgodaZFBV" +
                "Wx/GxBhsGpqKNuUsJ6qolFc9V6o2+nYY0dQyp5RHCXcoG5UqLU6pnbI+p7Aq//MoxEYXKS6oy8l4" +
                "AFnng8p7SopDSL2//p+l3/97di2LJtEy05CIZSM9mocECMELGbhzRvj8Sncoa3eXKE9qsvNRpHRd" +
                "EJGG//MoxCQM2R6oAAjScNiqxfWmCvc72Vf//////XX/3+8K81FEtzS0DPDDIWo2e1atgQwOj9w1" +
                "Eze0OPAFCDHdeWa5/9xQOKLD//MoxCsNGQaoAMPQcDZJeNFSjVCX8ko7+KfrJwnSCgcmBfaLb/8M" +
                "ayW4t8IS4stpcccuV2Vu1PXF/yIYnWqZoaw4C6hvq/////MoxDEN0NbEyp4Qcv//Q3//+mr65AhS" +
                "ABQ8BSiYd1uU0tt0BF8WK/OX7/n3IrGt7TevoLBmCUve6eIzY9///////od//TXG//MoxDQMmNKs" +
                "AKYWcMm6jwpIBMZGjQY1Vy/Odp1KlYoZ57jVKL3TaMJCq0fnQ4EoQjJTBC421DrebP3/4k8UYVaK" +
                "toUl5RnG//MoxDwMKLKcCIYYTSQDgnHTxGqDwyJSUtr4WI6wcujOIMXJxbWWSDPA+Y09YNosXAfa" +
                "Ss/01ZqXUkmvrnrw5bL0BcEnA2rk//MoxEYNCNaIAUl4ALYjqnZmEBoqQPLFmTO37Oqo3DgxCwvh" +
                "FKeaK1ntg61RZ9ukjmabd2pp0ECY4+WStXQv/r////7///qh//MoxEwTILZ8AZzIAPUToD8bhrdz" +
                "CCzALjTJASwA9EcNA7yIDYiEgwKYgcksZDcaV2pinmcMVA6FALFvAOKAYRGvU4+hqWAY//MoxDoX" +
                "8M6YK5rQAGqrvpNwykPUhPKlj72Nvn3Ybn9P////+/2NO+t2VMM9ZdDfGCSlEMAJM2AAGwH/z9XS" +
                "ZNgjqdm/kORb//MoxBUNILLqX89AAnr62/2zNax5Z9CzN1ThUWFmeC4wKf////////9h5d61MgJU" +
                "tAoAoH64fCoCQWLEVtAVAuO9Ysk38gYk//MoxBsMCM7Vv0gYAjOJDVg2alZUzJhLEv//Q///////" +
                "ySrH2YJ8Mieb7CxxIQYc6c2r3BhktHQzmNi1NjQFfU/nj5oozJdS//MoxCUV6h6gAZpoAL/dkG3/" +
                "rugX/r7czMDMxKjpIra7qS/5SKB50DQvoKnKarqf/8zUZBsOH5R2h//yBkIA5YCnxbokNHkZ//Mo" +
                "xAgNOaKwy9MoAAApkD2IPeNjEul1qKINzCIJ/iIDf8T//////6f9D/Sw0BBRWd/tUpwgDBTL1gIp" +
                "HIIBJIAB+oujlGkF//MoxA4L2aL2XmtElkM7VO1QxgRgk3f4+lxv9L//////0b/MAN8qgKO93Zsh" +
                "ojX9iScoAlkvLV7rEUBckWfanWFjJWZdXkqn//MoxBkNAaqoAJxKlP6lf/R/9CM6nz0Af1aUVKC/" +
                "8R+jxHcjCtiGEP1lRiHgZ8W/6xBgZf3uE2ITOhKpKKf6Cn2JIEDfdFEg//MoxCAKkSqoAJNKcNYw" +
                "7W/z///+S8VVX1zhmaAVGB2SIFjVx/jEFFjhiRpK2rD3VhpvdnP5lhM+2y7k4KbS1oE2nQUd+vLP" +
                "//MoxDANKQKYAJvMcP////yKB8bGtnIoNghKlHcQQQlti9h9fWNz3u+fyL3393hYPVyQJj9qsGWF" +
                "x1Euxb3yF////8ekMU3///MoxDYNKPqcyjmMcNJwFQlFZCioLFpI1vfeGdL+3t9uetjFxzQcWdrk" +
                "GCwJAyYMgzTXbsZ////3IWv1qvZiKcNGcUCNQGYU//MoxDwMCQKoAEjMcMeaXyI3N6IEUZeSA9Fh" +
                "Y1MkfKY53cihxCMfiVFdP///w/WlNz+olf1GZOKwA3jiVr+HUTBQdfl/zaCg//MoxEYMaQaoAChM" +
                "cJEfY+HFgfwEwR14+9iZDrJxe9FS6pdn/Z///+vYlf3HACSAjwX5P/FxWTicESXJ0QEg2BkEiZLk" +
                "Z/AI//MoxE8MYP64AGpWcAFg1k0rFQSAaHhLMTpDZXn69pYxMFUKFdttTbgAFAHqdQYuTkQODY71" +
                "VEDJOJANDwmH8nWximmokygW//MoxFgM2N7EAGpYcaTepNsFAiIWPCZ13//////qDbU5VwAWgDx8" +
                "IBKJygwNjMwADAYjj2tHvM/eqn3Iop9nsAzeR5iOfut0//MoxF8NQLsSXipSTo5f////9RRd1I2m" +
                "qQFABIAP2LIoFscZYGIDhRhTOJrLYooLCs3rKxbTTTtVlljUgidMgypH////+1fo//MoxGUNAPb+" +
                "PjjMcimoAMtFoA6gJAJwy6oBGGFdgrmWFb6pGUJuARwwr0neks/2iL7v//8kVMlgVDUNVVavxREI" +
                "0MjJQugb//MoxGwMQM7lnjDMcoThO9yTKIhEQyMjBGgbhfG70+J49JQ8ioVD2SD9Q/F7v//////6" +
                "hdVMQU1FMy45OS41VVVVVVVVVVVV//MoxHYLyLbFnhDGTlVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV" +
                "VVVVVVVVVVVVVVVVVVVVVVVMQU1FMy45OS41VVVVVVVVVVVV//MoxIEMkNXIAEpYcFVVVVVVVVVV" +
                "VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVMQU1FMy45OS41VVVVVVVVVVVV//MoxIkA" +
                "AANIAAAAAFVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVMQU1FMy45OS41" +
                "VVVVVVVVVVVV//MoxMQAAANIAAAAAFVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV" +
                "VVVVVVVMQU1FMy45OS41VVVVVVVVVVVV//MoxMQAAANIAAAAAFVVVVVVVVVVVVVVVVVVVVVVVVVV" +
                "VVVVVVVVVVVVVVVVVVVVVVVVVVVMQU1FMy45OS41VVVVVVVVVVVV//MoxMQAAANIAAAAAFVVVVVV" +
                "VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV//Mo" +
                "xMQAAANIAAAAAFVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV" +
                "VVVVVVVVVVVVVVVV//MoxMQAAANIAAAAAFVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV" +
                "VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV//MoxMQAAANIAAAAAFVVVVVVVVVVVVVVVVVVVVVV" +
                "VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV";


        String testStr = "xxxxxxx";

        final String reg = "^[A-Za-z0-9\\+/]+$";
        boolean matches = testStr.matches(reg);
        log.info("is base64 :{}",matches);



        int len = baseStr.length();

        log.info("base string length is :{}",len);
        int yu = len % 4;
        log.info("取余数：{}",yu);
        System.out.println(baseStr.toString());

    }




}
